package com.shj.controller;

import com.shj.entity.SysExpenses;
import com.shj.entity.SysPerson;
import com.shj.entity.SysPolicy;
import com.shj.service.SysExpensesService;
import com.shj.service.SysPersonService;
import com.shj.service.SysPolicyService;
import com.shj.util.MyIdUtil;
import com.sun.xml.internal.ws.client.sei.ResponseBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.sound.midi.Soundbank;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author songhuajian
 * @Description: TODO
 * @date 2020/6/17 21:57
 **/
@Controller
@RequestMapping("/biz/expenses")
public class SysExpensesController {
    @Autowired
    private SysPersonService sysPersonService;
    @Autowired
    private SysExpensesService sysExpensesService;
    @Autowired
    private SysPolicyService sysPolicyService;

    @RequestMapping("/open")
    public String open(Model model) {
        return "expenses/expenses_list";
    }

    @RequestMapping("/list")
    public String findAll(Model model) {
        List<SysExpenses> list = sysExpensesService.findAll();
        model.addAttribute("list", list);
        return "expenses/examine_list";
    }

    @RequestMapping("/examine")
    public String examine(Model model, Integer id) {
        SysExpenses sysExpenses = sysExpensesService.findById(id);
        sysExpenses.setStatus(1);
        sysExpensesService.edit(sysExpenses);
        return findAll(model);
    }

    @RequestMapping("/findByCardId")
    public String findByCardId(Model model, String cardId) {
        SysPerson sysPerson = sysPersonService.findByCardId(cardId);
        if (sysPerson == null) {
            model.addAttribute("msg", "用户不存在");
            return open(model);
        } else if (!"1".equals(sysPerson.getIsPart())) {
            model.addAttribute("msg", "该用户未参合");
            return open(model);
        }
        model.addAttribute("person", sysPerson);
        return "expenses/expenses_add";
    }

    @RequestMapping("/confirm")
    public String confirm(Model model, SysExpenses sysExpenses) {
        // 查询当前年度慢性病封顶线和报销比例
        int year = LocalDate.now().getYear();
        SysPolicy sysPolicy = sysPolicyService.findByYear(String.valueOf(year));
        // 预计算本次金额
        BigDecimal yujisuan = sysExpenses.getExFree().multiply(sysPolicy.getScale());
        // 计算当前年度已报销金额
        BigDecimal yibao = sysExpensesService.sumFree(sysExpenses.getPerCode());
        if (yibao == null) {
            yibao = new BigDecimal(0);
        }
        // 计算还能报销的金额
        BigDecimal haike = sysPolicy.getMaxLine().subtract(yibao);
        // 计算实际报销
        if (yujisuan.compareTo(haike) > 0) {
            sysExpenses.setExFree(haike);
        } else if (yujisuan.compareTo(haike) < 0 || yujisuan.compareTo(haike) == 0) {
            sysExpenses.setExFree(yujisuan);
        }
        model.addAttribute("expenses", sysExpenses);
        return "expenses/expenses_confirm";
    }

    @RequestMapping("/add")
    public String add(Model model, SysExpenses sysExpenses) {
        String exId = MyIdUtil.getTimeId();
        String year = String.valueOf(LocalDate.now().getYear());
        String time = LocalDateTime.now().toString();

        sysExpenses.setExpensesId(exId);
        sysExpenses.setExYear(year);
        sysExpenses.setExTime(time);
        sysExpenses.setStatus(0);

        sysExpensesService.add(sysExpenses);
        return open(model);
    }
}
